﻿Imports System.Collections.ObjectModel
Imports FL_8x0.Common
Imports FL_8x0.Common.Database
Imports FL_8x0.FL_800.Model
Imports FL_8x0.Model
Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.CommandWpf

Namespace FL_8x0.FL_800.ViewModel
    Public Class ReportViewModel
        Inherits ViewModelBase

        Private _applyCommand As RelayCommand(Of Window)               '点击“确认”触发命令
        Private _cancelCommand As RelayCommand(Of Window)              '点击“取消”触发命令

        Private _id As String                                         '当前product ID
        Private _gpUnit As String                                     '直压单位
        Private _leakUnit As String                                   '小漏单位

        Private _productService As New GroupModel

        Private _reportModel As ReportModel                            '报告

        Public Sub New()
            Version113Visable = New GridLength(2, GridUnitType.Star)
            _productService.IsMs = True
            If SystemData.Machine = Model_800 Then
                _productService.IsMs = False
                If SystemData.Version = Version_800_113 Then
                    Version113Visable = New GridLength(0)
                End If
            End If

            IdList = New ObservableCollection(Of String)(User_ListProductAndName())
            If g_udtReport.ReportId <> "" Then
                Id = g_udt800.Id & "  /  " & g_udt800.Name
            Else
                Id = IdList(0)
            End If

            '            Product = tmp.ReadProduct(g_dbConn测试记录, strId)
            '            GpUnit = Product.PressureUnit
            '            LeakUnit = Product.LeakUnit
            ReportModel = New ReportModel
            ApplyCommand = New RelayCommand(Of Window)(Sub(window) Apply(window))
            CancelCommand = New RelayCommand(Of Window)(Sub(window) Cancel(window))
        End Sub


        '=====================================================================
        '函 数 名: Apply
        '参    数: 
        '返 回 值:
        '功能描述: 报告存库，记录全局变量
        '=====================================================================
        Public Sub Apply(ByVal window As Window)
            ReportModel.ReportId = SystemData.idWorker.nextId()
            If ReportModel.ProductCode = "" Then
                ReportModel.ProductCode = "空"
            End If
            User_AddReport(ReportModel)
            g_udtReport = ReportModel
            g_udt800 = Product
            window.DialogResult = True
        End Sub

        '=====================================================================
        '函 数 名: Cancel
        '参    数: 
        '返 回 值:
        '功能描述: 取消
        '=====================================================================
        Private Sub Cancel(ByVal window As Window)
            If IsNothing(g_udtReport.ReportId) Then
                g_udtReport.ReportId = ""
            End If
            window.DialogResult = False
        End Sub


        Private _product As GroupModel

        Public Property Product() As GroupModel
            Get
                Return _product
            End Get
            Set(ByVal value As GroupModel)
                _product = value
                RaisePropertyChanged()
            End Set
        End Property


        Private _idList As ObservableCollection(Of String)
        Private _version113Visable As GridLength

        Public Property IdList() As ObservableCollection(Of String)
            Get
                Return _idList
            End Get
            Set(ByVal value As ObservableCollection(Of String))
                _idList = value
            End Set
        End Property

        Public Property ApplyCommand As RelayCommand(Of Window)
            Get
                Return _applyCommand
            End Get
            Set(value As RelayCommand(Of Window))
                _applyCommand = value
            End Set
        End Property

        Public Property CancelCommand As RelayCommand(Of Window)
            Get
                Return _cancelCommand
            End Get
            Set(value As RelayCommand(Of Window))
                _cancelCommand = value
            End Set
        End Property

        Public Property Id As String
            Get
                Return _id
            End Get
            Set(value As String)
                _id = value
                RaisePropertyChanged()
                Dim strId = value.Split(" / ")(0)
                Product = _productService.ReadProduct(g_dbConn测试记录, strId)
                GpUnit = Product.PressureUnit
                LeakUnit = Product.LeakUnit
            End Set
        End Property

        Public Property GpUnit As String
            Get
                Return _gpUnit
            End Get
            Set(value As String)
                _gpUnit = value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property LeakUnit As String
            Get
                Return _leakUnit
            End Get
            Set(value As String)
                _leakUnit = value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property ReportModel As ReportModel
            Get
                Return _reportModel
            End Get
            Set(value As ReportModel)
                _reportModel = value
            End Set
        End Property


        Public Property Version113Visable As GridLength
            Get
                Return _version113Visable
            End Get
            Set(value As GridLength)
                _version113Visable = value
                RaisePropertyChanged()
            End Set
        End Property
    End Class
End Namespace
